//
//  MKMyCollectionViewCell.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/10.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import UIKit

class MKMyCollectionViewCell: MKBaseTableViewCell,UICollectionViewDelegate,UICollectionViewDataSource {

    var collectionView:UICollectionView!
    var tipArr:Array<String>!
    var iconArr:Array<String>!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.makeUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func config(tipArr:Array<String>,iconArr:Array<String>) -> Void {
        self.tipArr = tipArr
        self.iconArr = iconArr
        self.setNeedsLayout()
    }
    
    func makeUI() -> Void {
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenWidth / 3.0, height: 100)
        self.collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        self.collectionView.backgroundColor = UIColor.clear
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.isScrollEnabled = false
        self.collectionView.register(MKMyCollectionViewSubCell.classForCoder(), forCellWithReuseIdentifier: "MKMyCollectionViewSubCell")
        self.contentView.addSubview(self.collectionView)
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.tipArr.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MKMyCollectionViewSubCell", for: indexPath) as! MKMyCollectionViewSubCell
        cell.config(icon: self.iconArr[indexPath.item], tip: self.tipArr[indexPath.item], vHiden: true)
        return cell
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.collectionView.frame = self.contentView.bounds
    }
}

class MKMyCollectionViewSubCell: UICollectionViewCell {

    var icon:UIImageView!
    var tip:UILabel!
    var vLine:UIView!
    var hLine:UIView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.icon = UIImageView.init()
        self.contentView.addSubview(self.icon)
        
        self.tip = UILabel.label(fontSize: 12, textColor: UIColorFromRGB(rgbValue: 0x333333, A: 1))
        self.tip.textAlignment = .center
        self.contentView.addSubview(self.tip)
        
        self.hLine = UIView.init()
        self.hLine.backgroundColor = UIColorFromRGB(rgbValue: 0xe5e5e5, A: 1)
        self.contentView.addSubview(self.hLine)
        
        self.vLine = UIView.init()
        self.vLine.backgroundColor = UIColorFromRGB(rgbValue: 0xe5e5e5, A: 1)
        self.contentView.addSubview(self.vLine)
    }
    func config(icon:String,tip:String,vHiden:Bool) -> Void {
        self.icon.image = UIImage.init(named: icon)
        self.tip.text = tip
        self.setNeedsLayout()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.tip.sizeToFit()
        self.icon.frame = CGRect.init(x: (self.contentView.width - 42 ) * 0.5, y: (self.contentView.height - 42 - self.tip.height - 6) * 0.5 - 5, width: 42, height: 42)
        self.tip.frame = CGRect.init(x: (self.contentView.width - self.tip.width) * 0.5, y: self.icon.bottom + 6, width: self.tip.width, height: self.tip.height)
        self.vLine.frame = CGRect.init(x: self.contentView.right - 0.5, y: 8, width: 0.5, height: self.contentView.height - 16)
        self.hLine.frame = CGRect.init(x: 0, y: self.contentView.bottom - 0.5, width: self.contentView.width, height: 0.5)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
